package com.sinosoft.dsp.component.bpm.viewbean.transition.part;

import org.jbpm.xsdobject.TransitionType;

import com.sinosoft.dsp.component.bpm.util.ActivityType;
import com.sinosoft.dsp.component.bpm.util.ProcessParse;
import com.sinosoft.dsp.component.bpm.viewbean.BpmJpdlDefineViewBean;
import com.sinosoft.dsp.component.bpm.viewbean.transition.BpmAbstractTransitionPartEditViewBean;
import com.sinosoft.dsp.component.bpm.viewbean.transition.IBpmTransitionPartEditViewBean;
import com.sinosoft.dsp.system.core.exception.SinoException;
import com.sinosoft.dsp.system.to.IGenericTO;
import com.sinosoft.dsp.system.to.attribute.Attribute;
import com.sinosoft.dsp.system.to.helper.ToHelper;

public class BpmTransitionCommonBaseViewBean extends
		BpmAbstractTransitionPartEditViewBean implements
		IBpmTransitionPartEditViewBean {

	// 转移名称
	public static final String FIELD_NAME = "name";
	public static final String FIELD_INIT_NAME = "initName";

	// 源节点
	public static final String FIELD_FROM_ACTIVITY_NAME = "fromActivityName";

	public static final String FIELD_FROM_ACTIVITY_TYPE = "fromActivityType";

	// 转移节点
	public static final String FIELD_TO_ACTIVITY_NAME = "toActivityName";

	public static final String FIELD_TO_ACTIVITY_TYPE = "toActivityType";

	// 活动描述
	public static final String FIELD_DESC = "description";

	@Override
	public void init(IGenericTO tos) throws SinoException {
		super.init(tos);

		// name
		Attribute<?> name = ToHelper.getContentAttriubteByName(tos, FIELD_NAME);
		String nameStr = name.convertToString();
		this.addField(FIELD_NAME, nameStr);
		this.addField(FIELD_INIT_NAME, nameStr);

		// 源活动节点名称
		Attribute<?> fromActivityName = ToHelper.getContentAttriubteByName(tos,
				FIELD_FROM_ACTIVITY_NAME);
		String fromActivityNameStr = fromActivityName.convertToString();
		this.addField(FIELD_FROM_ACTIVITY_NAME, fromActivityNameStr);

		// 转移活动节点名称
		Attribute<?> toActivityName = ToHelper.getContentAttriubteByName(tos,
				FIELD_TO_ACTIVITY_NAME);
		String toActivityNameStr = toActivityName.convertToString();
		this.addField(FIELD_TO_ACTIVITY_NAME, toActivityNameStr);

		BpmJpdlDefineViewBean pvb = (BpmJpdlDefineViewBean) this.getParent()
				.getParent();
		org.jbpm.xsdobject.Process process = pvb.getProcess();
		Object fromAct = ProcessParse.getActivity(process, fromActivityNameStr);
		Object toAct = ProcessParse.getActivity(process, toActivityNameStr);
		// 如果无法获得对象，则创建一个新的对象
		if (fromAct == null) {
			Attribute<?> fromActivityType = ToHelper.getContentAttriubteByName(
					tos, FIELD_FROM_ACTIVITY_TYPE);
			ActivityType at = ActivityType.getActivityType(fromActivityType
					.convertToString());
			fromAct = at.createObject(fromActivityNameStr);
			process.getDescriptionAndSwimlaneAndOn().add(fromAct);
		}

		if (toAct == null) {
			Attribute<?> toActivityType = ToHelper.getContentAttriubteByName(
					tos, FIELD_TO_ACTIVITY_TYPE);
			ActivityType at = ActivityType.getActivityType(toActivityType
					.convertToString());
			toAct = at.createObject(toActivityNameStr);
			process.getDescriptionAndSwimlaneAndOn().add(toAct);
		}

		ActivityType fromActivityType = ActivityType.getActivityType(fromAct
				.getClass());
		this.addField(FIELD_FROM_ACTIVITY_TYPE, fromActivityType.getTagName());

		ActivityType toActivityType = ActivityType.getActivityType(toAct
				.getClass());
		this.addField(FIELD_TO_ACTIVITY_TYPE, toActivityType.getTagName());

		TransitionType tt = ActivityType.createOrUpdateActivityTransition(
				fromAct, nameStr, toActivityNameStr, "");

		if (tt.getDescription().size() > 0) {
			this.addField(FIELD_DESC, tt.getDescription().get(0));
		}
	}
}
